package com.tiantian.quanminxiu.module;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.jakewharton.rxbinding.view.RxView;
import com.nineoldandroids.animation.Animator;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.utils.APPUtils;

/*
* 头部模块
* */
public class Module_Header extends LinearLayout {

    //region 初始化成员和视图组件
    public TextView txtHeader_Title,txtHeader_TitleDown,txtHeader_TitleDownFlag;
    public TextView txtHeader_Anchor,txtHeader_Regal,txtHeader_Popularity,txtNotDate;
    public View txtHeader_Back,txtHeader_Edit,txtHeader_Enter,vHeader_TitleDown,vHeader_TypeList,vTypeMark,txtSearchTitle,rlSearchContent,txtHeader_Search,txtRegister,txtPreviousStep;
    private Context thisContext;
    private Activity thisActivity;
    public EditText etSearchContent;
    private View vLoading, vLoadingMark,vHeaderNavigation;
    //endregion

    //region 构造器，带一个参数和两个参数
    public Module_Header(Context context) {
        this(context, null);
        thisContext = context;
        thisActivity = (Activity) context;
    }

    public Module_Header(Context context, AttributeSet attrs) {
        super(context, attrs);
        thisContext = context;
        thisActivity = (Activity) context;
        LayoutInflater.from(context).inflate(R.layout.module_header, this, true);

        //显示无数据
        txtNotDate = (TextView) findViewById(R.id.txtNotDate);
        //Loading
        vLoading = findViewById(R.id.vLoading);
        vLoadingMark = findViewById(R.id.vLoadingMark);

        txtHeader_Title = (TextView) findViewById(R.id.txtHeader_Title);
        txtHeader_TitleDown = (TextView) findViewById(R.id.txtHeader_TitleDown);
        txtHeader_Anchor = (TextView) findViewById(R.id.txtHeader_Anchor);
        txtHeader_Regal = (TextView) findViewById(R.id.txtHeader_Regal);
        txtHeader_Popularity = (TextView) findViewById(R.id.txtHeader_Popularity);
        txtHeader_TitleDownFlag = (TextView) findViewById(R.id.txtHeader_TitleDownFlag);
        txtHeader_Search = findViewById(R.id.txtHeader_Search);
        txtRegister = findViewById(R.id.txtRegister);
        txtPreviousStep = findViewById(R.id.txtPreviousStep);
        vHeaderNavigation = findViewById(R.id.vHeaderNavigation);
        vHeader_TypeList = findViewById(R.id.vHeader_TypeList);
        vHeader_TitleDown = findViewById(R.id.vHeader_TitleDown);
        vTypeMark = findViewById(R.id.vTypeMark);
        etSearchContent = (EditText) findViewById(R.id.etSearchContent);

        txtHeader_Back = findViewById(R.id.txtHeader_Back);
        rlSearchContent = findViewById(R.id.rlSearchContent);
        txtSearchTitle = findViewById(R.id.txtSearchTitle);
        txtHeader_Edit = findViewById(R.id.txtHeader_Edit);
        txtHeader_Enter = findViewById(R.id.txtHeader_Enter);
        RxView.clicks(vLoadingMark).subscribe(aVoid -> CommonUtils.showToask(thisContext, "数据加载中..."));

        ResetView(context.obtainStyledAttributes(attrs, R.styleable.Module_Header));

        txtHeader_Back.setOnClickListener(Button_OnClickListener);
        txtSearchTitle.setOnClickListener(Button_OnClickListener);
        txtHeader_Edit.setOnClickListener(Button_OnClickListener);
        txtHeader_Search.setOnClickListener(Button_OnClickListener);
        txtRegister.setOnClickListener(Button_OnClickListener);
        vHeader_TitleDown.setOnClickListener(Button_OnClickListener);
        vTypeMark.setOnClickListener(Button_OnClickListener);
    }
    //endregion

    public void ResetView() {
        ResetView(null);
    }

    //region 初始化布局
    public void ResetView(TypedArray Parm) {
        String strTitle = Parm == null ? getResources().getString(R.string.app_name) : Parm.getString(R.styleable.Module_Header_header_title);
        if (strTitle != null) {
            txtHeader_Title.setText(strTitle);
        }
        txtHeader_Back.setVisibility(Parm.getBoolean(R.styleable.Module_Header_button_back_display, false) ? VISIBLE : GONE);
        vHeaderNavigation.setVisibility(Parm.getBoolean(R.styleable.Module_Header_header_navigation, true) ? VISIBLE : GONE);
        txtHeader_Edit.setVisibility(Parm.getBoolean(R.styleable.Module_Header_button_edit_display, false) ? VISIBLE : GONE);
        txtHeader_Search.setVisibility(Parm.getBoolean(R.styleable.Module_Header_button_search_display, false) ? VISIBLE : GONE);
        txtRegister.setVisibility(Parm.getBoolean(R.styleable.Module_Header_button_register_display, false) ? VISIBLE : GONE);
        txtPreviousStep.setVisibility(Parm.getBoolean(R.styleable.Module_Header_button_previous_display, false) ? VISIBLE : GONE);
        txtHeader_Enter.setVisibility(Parm.getBoolean(R.styleable.Module_Header_button_enter_display, false) ? VISIBLE : GONE);
        if (Parm.getBoolean(R.styleable.Module_Header_search_display, false)) {
            txtHeader_Title.setVisibility(View.GONE);
            rlSearchContent.setVisibility(View.VISIBLE);
        }
        if (Parm.getString(R.styleable.Module_Header_header_titledown)!=null) {
            txtHeader_Title.setVisibility(View.GONE);
            vHeader_TitleDown.setVisibility(View.VISIBLE);
            txtHeader_TitleDown.setText(Parm.getString(R.styleable.Module_Header_header_titledown));
        }
    }
    //endregion

    //region 注册头部button点击监听  Button_OnClickListener
    private OnClickListener Button_OnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.txtSearchTitle:
                case R.id.txtHeader_Back:
                    thisActivity.finish();
                    break;
                case R.id.txtHeader_Edit:
                    APPUtils.EditInfo_TO(thisActivity);
                    break;
                case R.id.txtHeader_Search:
                    APPUtils.Search_TO(thisActivity);
                    break;
                case R.id.txtRegister:
                    APPUtils.Register_To(thisActivity);
                    break;
                case R.id.vHeader_TitleDown:
                    if(vHeader_TypeList.getVisibility()==View.VISIBLE){
                        hideTpyeList();
                    }else {
                        showTpyeList();
                    }
                    break;
                case R.id.vTypeMark:
                    hideTpyeList();
                    break;
            }
        }
    };
    //endregion

    //region 展示类型列表 showTpyeList
    public void showTpyeList(){
        vHeader_TypeList.setVisibility(View.VISIBLE);
        txtHeader_TitleDownFlag.setText("{fa-angle-up}");
    }
    //endregion

    //region 展示类型列表 hideTpyeList
    public void hideTpyeList(){
        vHeader_TypeList.setVisibility(View.GONE);
        txtHeader_TitleDownFlag.setText("{fa-angle-down}");
    }
    //endregion

    //region 显示无数据 NotDate_Show（）
    public void NotDate_Show() {
        txtNotDate.setVisibility(View.VISIBLE);
    }
    //endregion

    //region 显示无数据 NotDate_Show（）
    public void NotDate_Show(String detail) {
        txtNotDate.setVisibility(View.VISIBLE);
        txtNotDate.setText(detail);
    }
    //endregion

    //region 隐藏无数据 NotDate_Hide()
    public void NotDate_Hide() {
        txtNotDate.setVisibility(View.GONE);
    }
    //endregion

    //region 头部Loading动画显示   Loading_Show（）
    public void Loading_Show() {
        Loading_Show(false);
    }
    //endregion

    //region Loading_Show(boolean LoadingMark)
    public void Loading_Show(boolean LoadingMark) {
        YoYo.with(Techniques.FadeIn).duration(200).playOn(vLoading);
        vLoading.setVisibility(View.VISIBLE);
        vLoadingMark.setVisibility(LoadingMark ? View.VISIBLE : View.GONE);
    }
    //endregion

    //region 隐藏加载动画  Loading_Hide()
    public void Loading_Hide() {
        if (vLoading.getVisibility() == View.GONE) {
            return;
        }
        YoYo.with(Techniques.FadeOut).duration(700).withListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                vLoading.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        }).playOn(vLoading);
        vLoadingMark.setVisibility(View.GONE);
    }
    //endregion
}